﻿using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using DeviceDebugger.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.ObjectModel;
using DeviceDebugger.DataAccess;
using GalaSoft.MvvmLight.Messaging;
using System.Windows;

namespace DeviceDebugger.ViewModel
{
    public class StorageDisableViewModel : ViewModelBase
    {
        private int _rowNum;
        public int RowNum
        {
            get { return _rowNum; }
            set { _rowNum = value; RaisePropertyChanged(() => RowNum); }
        }

        private int _columNum;
        public int ColumNum
        {
            get { return _columNum; }
            set { _columNum = value; RaisePropertyChanged(() => ColumNum); }
        }

        private int _LayerNum;
        public int LayerNum
        {
            get { return _LayerNum; }
            set { _LayerNum = value; RaisePropertyChanged(() => LayerNum); }
        }

        private List<int> _rowList;
        /// <summary>
        /// 排编号集合，用于页面绑定下拉框
        /// </summary>
        public List<int> RowList
        {
            get { return _rowList; }
            set { _rowList = value; RaisePropertyChanged(() => RowList); }
        }
        private List<int> _columList;
        /// <summary>
        /// 列编号集合，用于页面绑定下拉框
        /// </summary>
        public List<int> ColumList
        {
            get { return _columList; }
            set { _columList = value; RaisePropertyChanged(() => ColumList); }
        }
        private List<int> _layerList;
        /// <summary>
        /// 层编号集合，用于页面绑定下拉框
        /// </summary>
        public List<int> LayerList
        {
            get { return _layerList; }
            set { _layerList = value; RaisePropertyChanged(() => LayerList); }
        }



        private ObservableCollection<StorageDisableModel> _listStorageDisable;
        public ObservableCollection<StorageDisableModel> ListStorageDisable
        {
            get { return _listStorageDisable; }
            set { _listStorageDisable = value; RaisePropertyChanged(() => ListStorageDisable); }
        }
        private StorageDisableModel _selectedItemModel;
        public StorageDisableModel SelectedItemModel
        {
            get { return _selectedItemModel; }
            set { _selectedItemModel = value; RaisePropertyChanged(() => SelectedItemModel); }
        }


        public StackerModel StackerModel { get; set; }
        public RelayCommand AddStorageDisableCommand => new RelayCommand(AddStorageDisableCommand_Click);
        public RelayCommand DeleteStorageDisableCommand => new RelayCommand(DeleteStorageDisableCommand_Click);


        public RelayCommand<Window> CloseCommand => new RelayCommand<Window>(CloseCommand_Click);


        public StorageDisableViewModel()
        {
            Messenger.Default.Register<StackerModel>(this, "StorageDisable", (Model) =>
            {
                if (Model is null)
                    return;
                StackerModel = Model;
                RowList = new();
                ColumList = new();
                LayerList = new();
                for (int i = 1; i <= Model.RowCount; i++)
                {
                    RowList.Add(i);
                }
                RowNum = 1;
                for (int i = 1; i <= Model.ColumCount; i++)
                {
                    ColumList.Add(i);
                }
                ColumNum = 1;
                for (int i = 1; i <= Model.LayerCount; i++)
                {
                    LayerList.Add(i);
                }
                LayerNum = 1;
                ListStorageDisable = new(DataVisitor.GetStorageDisables(Model.Id));
            });
        }

        private void AddStorageDisableCommand_Click()
        {
            if (StackerModel is not null)
            {
                StorageDisableModel model = new();
                model.StackerId = StackerModel.Id;
                model.Location = string.Format("{0}-{1}-{2}", RowNum.ToString().PadLeft(2, '0'), ColumNum.ToString().PadLeft(2, '0'), LayerNum.ToString().PadLeft(2, '0'));
                if (ListStorageDisable.Where(a => a.Location == model.Location).Count() == 0)
                {
                    int id = DataVisitor.AddStorageDisable(model);
                    if (id > 0)
                    {
                        model.Id = id;
                        ListStorageDisable.Add(model);
                    }
                }
            }
        }

        private void DeleteStorageDisableCommand_Click()
        {
            if (SelectedItemModel != null)
            {
                if (DataVisitor.DeleteStorageDisable(SelectedItemModel.Id))
                {
                    ListStorageDisable.Remove(SelectedItemModel);
                }
            }
        }

        private void CloseCommand_Click(Window obj)
        {
            obj.Close();
        }
    }
}
